//
//  ECCollectionViewLayoutAttributes.m
//  collectionView_test00
//
//  Created by stone on 16/12/17.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNCollectionViewLayoutAttributes.h"

@implementation SNCollectionReusableView

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    
    SNCollectionViewLayoutAttributes *ecLayoutAttributes = (SNCollectionViewLayoutAttributes*)layoutAttributes;
    self.backgroundColor = ecLayoutAttributes.color;
}

@end

@implementation SNCollectionViewLayoutAttributes
+ (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath *)indexPath {
    
    SNCollectionViewLayoutAttributes *layoutAttributes = [super layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
    
    //    if (indexPath.section%2 == 0) {
    //        layoutAttributes.color = [UIColor redColor];
    //    } else {
    //        layoutAttributes.color = [UIColor blueColor];
    //    }
    return layoutAttributes;
}
@end
//sn_note:=========  ============================ stone 🐳 ===========/

@implementation SNCollectionViewLayout

+ (Class)layoutAttributesClass
{
    /** readOnly 还能这么改... */
    return [SNCollectionViewLayoutAttributes class];
}

- (void)prepareLayout {
    [super prepareLayout];
    
    /** 垂直方向 间距 */
    self.minimumLineSpacing = self.sn_minimumLineSpacing;
    /** 横向方向 间距 */
    self.minimumInteritemSpacing = self.sn_minimumInteritemSpacing;
    /** 内缩 内容 */
    self.sectionInset = self.sn_sectionInset;
    /** item大小 */
    self.itemSize = self.sn_itemSize;
    
    /** 注册 */
    [self registerClass:[SNCollectionReusableView class] forDecorationViewOfKind:NSStringFromClass([SNCollectionViewLayoutAttributes class])];
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
    
    NSMutableArray *allAttributes = [NSMutableArray arrayWithArray:attributes];
    
    for (UICollectionViewLayoutAttributes *attribute in attributes) {
        
        // Look for the first item in a row
        if (attribute.representedElementKind == UICollectionElementCategoryCell && attribute.frame.origin.x == self.sectionInset.left) {
            
            // Create decoration attributes
            SNCollectionViewLayoutAttributes *decorationAttributes = [SNCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:NSStringFromClass([SNCollectionViewLayoutAttributes class]) withIndexPath:attribute.indexPath];
            
            if ([self.delegate respondsToSelector:@selector(setCollectionViewSectionBackGroundColor:)]) {
                UIColor * color = [self.delegate setCollectionViewSectionBackGroundColor:attribute.indexPath];
                decorationAttributes.color = color;
            }
            
            
            // Make the decoration view span the entire row (you can do item by item as well. I just
            // chose to do it this way)
            decorationAttributes.frame = CGRectMake(0, attribute.frame.origin.y-(self.sectionInset.top), self.collectionViewContentSize.width, self.itemSize.height + (self.minimumLineSpacing + self.sectionInset.top + self. sectionInset.bottom));
            
            // Set the zIndex to be behind the item
            decorationAttributes.zIndex = attribute.zIndex-1;
            
            // Add the attribute to the list
            [allAttributes addObject:decorationAttributes];
        }
        
    }
    
    return allAttributes;
}

@end
